package cn.agiledata.bank.linkbank.impl.cmb;

import java.math.BigDecimal;
import java.util.Date;

import cn.agiledata.bank.common.util.DateUtil;
import cn.agiledata.bank.linkbank.impl.AbstractBankNetPayRequest;
import cn.agiledata.bank.linkbank.impl.AbstractBankPayRequest;
import cn.agiledata.bank.linkbank.logic.OrderIdTransformer;

/**
 * 中国招商银行网上银行支付请求
 * 
 * @author 马贤波
 * 
 */
public final class CmbNetPayRequest extends AbstractBankNetPayRequest {
	private OrderIdTransformer orderIdTransformer;

	public CmbNetPayRequest() {
		super();
	}

	protected AbstractBankPayRequest getCopiedInstance() {
		CmbNetPayRequest request = new CmbNetPayRequest();
		request.orderIdTransformer = this.orderIdTransformer;
		return request;
	}

	private OrderIdTransformer getOrderIdTransformer() {
		if (this.orderIdTransformer == null) {
			this.orderIdTransformer = new CmbOrderIdTransformer(getDate());
		}
		return this.orderIdTransformer;
	}

	public void setBranchId(String branchId) {
		setParameter(CmbNetConstant.BRANCH_ID, branchId);
	}

	public void setMerchantId(String merchantId) {
		setParameter(CmbNetConstant.MERCHANT_ID, merchantId);
	}

	public String getMerchantUrl() {
		return getParameter(CmbNetConstant.MERCHANT_URL);
	}

	public void setMerchantUrl(String merchantUrl) {
		setParameter(CmbNetConstant.MERCHANT_URL, merchantUrl);
	}

	public BigDecimal getAmount() {
		try {
			return new BigDecimal(getParameter(CmbNetConstant.AMOUNT));
		} catch (Exception e) {
			return null;
		}
	}

	public void setAmount(BigDecimal amount) {
		setParameter(CmbNetConstant.AMOUNT, amount.setScale(2).toString());
	}

	public Date getDate() {
		String dateValue = getParameter(CmbNetConstant.DATE);
		return DateUtil.string2Date(dateValue, CmbNetConstant.DATE_FORMAT);
	}

	public void setDate(Date date) {
		String dateValue = DateUtil.date2String(date,
				CmbNetConstant.DATE_FORMAT);
		setParameter(CmbNetConstant.DATE, dateValue);
	}

	public void setOrderId(String orderId) {
		// 将交易编号作为商户参数发送给招商银行
		setParameter(CmbNetConstant.MERCHANT_PARAMETER, orderId);
		// 生成招商银行要求的格式的订单编号
		orderId = getOrderIdTransformer().transId2OrderId(orderId);
		setParameter(CmbNetConstant.ORDER_ID, orderId);
	}

}
